package org.jeecg.modules.zhima.center.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import org.jeecg.modules.zhima.base.common.ResultCode;
import org.jeecg.modules.zhima.base.exception.Asserts;
import org.jeecg.modules.zhima.center.entity.UmsMemberReceiveAddress;
import org.jeecg.modules.zhima.center.mapper.UmsMemberReceiveAddressMapper;
import org.jeecg.modules.zhima.center.service.IUmsMemberReceiveAddressService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * @Description: ums_member_receive_address
 * @Author: jeecg-boot
 * @Date:   2020-08-27
 * @Version: V1.0
 */

@Service
public class UmsMemberReceiveAddressServiceImpl extends ServiceImpl<UmsMemberReceiveAddressMapper, UmsMemberReceiveAddress> implements IUmsMemberReceiveAddressService {

    @Override
    public UmsMemberReceiveAddress addOrUpdate(UmsMemberReceiveAddress address) {
        if (address.getDefaultStatus() == 1){
            this.resetDefaultStatus(address.getMemberId());
        }
        if (ObjectUtils.isEmpty(address.getId()) || address.getId() == 0){
            super.save(address);
        }else {
            UmsMemberReceiveAddress temp = super.getById(address.getId());
            if (temp == null){
                Asserts.fail(ResultCode.VALIDATE_FAILED);
            }
            super.updateById(address);
        }
        return address;
    }

    private boolean resetDefaultStatus(Integer memberId){
        if (ObjectUtils.isEmpty(memberId)){
            return false;
        }
        List<UmsMemberReceiveAddress> addresses = list(new LambdaQueryWrapper<UmsMemberReceiveAddress>()
                .eq(UmsMemberReceiveAddress::getMemberId, memberId)
                .eq(UmsMemberReceiveAddress::getDefaultStatus, 1));
        if (CollectionUtils.isNotEmpty(addresses)){
            addresses.stream().forEach(umsMemberReceiveAddress -> {
                umsMemberReceiveAddress.setDefaultStatus(0);
                super.updateById(umsMemberReceiveAddress);
            });
        }
        return true;
    }

    @Override
    public boolean add(Integer memberId, UmsMemberReceiveAddress address) {
        address.setMemberId(memberId);
        return this.save(address);
    }

    @Override
    public boolean delete(Integer memberId, Integer id) {
        return this.remove(new LambdaQueryWrapper<UmsMemberReceiveAddress>().eq(UmsMemberReceiveAddress::getMemberId, memberId).eq(UmsMemberReceiveAddress::getId, id));
    }

    @Override
    public boolean update(Integer memberId, Integer id, UmsMemberReceiveAddress address) {
        address.setId(null);
        if(address.getDefaultStatus()==1){
            //先将原来的默认地址去除
            UmsMemberReceiveAddress record= new UmsMemberReceiveAddress();
            record.setDefaultStatus(0);
            update(record, new LambdaQueryWrapper<UmsMemberReceiveAddress>().eq(UmsMemberReceiveAddress::getMemberId, memberId)
                    .eq(UmsMemberReceiveAddress::getId, id));
        }
        return super.update(address,
                new LambdaQueryWrapper<UmsMemberReceiveAddress>()
                        .eq(UmsMemberReceiveAddress::getMemberId, memberId)
                        .eq(UmsMemberReceiveAddress::getId, id)
        );
    }

    @Override
    public List<UmsMemberReceiveAddress> list(Integer memberId) {
        return list(new LambdaQueryWrapper<UmsMemberReceiveAddress>().eq(UmsMemberReceiveAddress::getMemberId, memberId));
    }

    @Override
    public List<UmsMemberReceiveAddress> setDefault(Integer id, Integer memberId) {
        List<UmsMemberReceiveAddress> addressList = this.list(memberId);
        for (UmsMemberReceiveAddress address: addressList) {
            if (!address.getId().equals(id) && address.getDefaultStatus() == 1){
                address.setDefaultStatus(0);
                updateById(address);
            }else if (address.getId().equals(id) && address.getDefaultStatus() == 0){
                address.setDefaultStatus(1);
                updateById(address);
            }
        }
        return addressList;
    }

    @Override
    public UmsMemberReceiveAddress getItem(Integer memberId, Integer id) {
        UmsMemberReceiveAddress address = getById(id);
        if (address != null && address.getMemberId().equals(memberId)){
            return address;
        }
        return null;
    }

}
